/**
 *  *2010-3-26 下午01:25:11  add 
 */
package com.base.myproject.client.ui;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.base.myproject.client.listener.HeardBeatEvent;
import com.base.myproject.client.listener.HeardBeatListener;
import com.base.myproject.client.tools.Debug;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.rpc.AsyncCallback;

/**
 * @author Administrator 2010-3-26 javazgw@gmail.com
 */
public class BaseHeartBeatX implements HeardBeatListener {
	Map<String, List<HeardBeatListener>> listeners = new HashMap<String, List<HeardBeatListener>>();
	static BaseHeartBeatX bhb = null;

	private BaseHeartBeatX() {
		final com.base.myproject.client.tools.GreetingServiceAsync greetingService = GWT
		.create(com.base.myproject.client.tools.GreetingService.class);
		AsyncCallback<HashMap<String,String>> a = new AsyncCallback<HashMap<String,String>>() {

			@Override
			public void onFailure(Throwable caught) {

			}

			@Override
			public void onSuccess(final HashMap<String,String> result) {
				Debug.printf(result.toString());
				//GWT.log("result" + result, null);
				Timer timer = new Timer() {

					public void run() {


						
						for(String key:result.keySet())
						{
							fireEvent(key, new HeardBeatEvent(this,result.get(key)));
						}
//						if(result.get("Reservation")!=null)
//							fireEvent("Reservation", new HeardBeatEvent(this,result.get("Reservation")));
						newinit();
						this.cancel();
					};
				};
				timer.schedule(2000);
				// timer.run();
			}

			private void newinit() {
				
				greetingService.getComet(this);
			}

		};
		
		greetingService.getComet(a);
	}

	static public BaseHeartBeatX getinstance() {
		if (bhb == null) {

			bhb = new BaseHeartBeatX();

		}
		return bhb;

	}

	@Override
	public void HeardBeatEventHandle(HeardBeatEvent evt) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onBrowserEvent(Event event) {
		// TODO Auto-generated method stub

	}

	public boolean fireEvent(String eventType, HeardBeatEvent be) {
		if (listeners != null) {

			List<HeardBeatListener> list = listeners.get(eventType);
			if (list != null) {
				for (HeardBeatListener l : list) {

					l.HeardBeatEventHandle(be);
				}
			}

		}
		return true;
	}

	public void addHeardBeatListener(String eventType,
			HeardBeatListener listener) {
		if (listener == null)
			return;
		if (listeners == null) {
			listeners = new HashMap<String, List<HeardBeatListener>>();
		}
		List<HeardBeatListener> list = listeners.get(eventType);
		if (list == null) {
			list = new ArrayList<HeardBeatListener>();
			listeners.put(eventType, list);
		}

		if (!list.contains(listener)) {
			list.add(listener);
		}

	}

	public void removeHeardBeatListener(final int eventType,
			final HeardBeatListener listener) {
		if (listeners == null) {
			return;
		}

		List<HeardBeatListener> list = listeners.get(eventType);
		if (list != null) {
			list.remove(listener);
			if (list.isEmpty()) {
				listeners.remove(eventType);
			}
		}

	}

}
